import pandas as pd
import numpy as np
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
# from pyecharts.globals import ThemeType


class BarChart_flow_workday_weekend_1year():
    
    def __init__(self,dataset, place=0, year='2017'):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        # self.flow=[];
        self.year=year
        
        #主程序入口
        self.main()
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘制条形图函数
        self.paintBarChart()
        
    
    #获取数据
    def getData(self):
            
        df=self.paint_data.loc[:,['date','flow']] #取出数据集中的日期和客流数据
        # print(type(df['date']))
        # df.columns = ['date','flow'] #添加表头名称

        df['date'] = pd.to_datetime(df['date']) #将数据类型转换为日期类型
        df = df.set_index('date') # 将date设置为index
        self.paint_data=df[self.year] #筛选某一年的天气数据
        # print(self.paint_data)             
      
    
    def paintBarChart(self):
        
        if self.place==0:
            title="九寨沟"+self.year+"年每月工作日与双休日平均客流量"
        else:
            title="四姑娘山"+self.year+"年每月工作日与双休日平均客流量"
        
        fileName='3.4 (L1400)'+title+"条形图.html"
        
        flow_workday_sums=[0,0,0,0,0,0,0,0,0,0,0,0,0]
        flow_weekend_sums=[0,0,0,0,0,0,0,0,0,0,0,0,0]
        
        flow_workday_avgs=[0,0,0,0,0,0,0,0,0,0,0,0,0]
        flow_weekend_avgs=[0,0,0,0,0,0,0,0,0,0,0,0,0]
        
        month_workday_nums = [0,0,0,0,0,0,0,0,0,0,0,0,0]
        month_weekend_nums = [0,0,0,0,0,0,0,0,0,0,0,0,0]            
        
        for i in range(1,13):
            daynums = 0
            
            format=self.year+'-{}'
            for day in self.paint_data[format.format(i)].index:
                daynums+=1
                if day.weekday()>0 and day.weekday()<6:
                    flow_workday_sums[i]+=int(self.paint_data[format.format(str(i)+'-'+str(daynums)):format.format(str(i)+'-'+str(daynums))].values)
                    #print(int(df['2018-{}'.format(str(i)+'-'+str(daynums)):'2018-{}'.format(str(i)+'-'+str(daynums))].values))
                    month_workday_nums[i]+=1
                    
                else:
                    flow_weekend_sums[i] +=int(self.paint_data[format.format(str(i)+'-'+str(daynums)):format.format(str(i)+'-'+str(daynums))].values)
                    month_weekend_nums[i]+=1
                    
            flow_workday_avgs[i]=flow_workday_sums[i]/month_workday_nums[i]
            flow_weekend_avgs[i]=flow_weekend_sums[i]/month_weekend_nums[i]
        
        #处理平均值为int值
        for i in range(1,13):
            flow_workday_avgs[i] = int( flow_workday_avgs[i])
            flow_weekend_avgs[i] = int( flow_weekend_avgs[i])
        
        flow_workday_avgs=flow_workday_avgs[1:13]
        flow_weekend_avgs=flow_weekend_avgs[1:13]
        # # 将工作日和周六日平均客流合并为一个DataFrame 
        # flow_avg = pd.DataFrame({'工作日':flow_workday_avgs[1:13],'双休日':flow_weekend_avgs[1:13]})
        
        # flow_avg.columns = ['工作日','双休日'] #设置表头名称
        labels = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'] #设置dataFrame的索引
        # flow_avg.index=index #设置索引
     
        c = (
            # Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"))
            Bar(init_opts=opts.InitOpts(width="1400px", height="600px"))
            .add_xaxis(labels)
            .add_yaxis("工作日", flow_workday_avgs)
            .add_yaxis("双休日", flow_weekend_avgs)
            .set_global_opts(
                title_opts=opts.TitleOpts(title=title),
                toolbox_opts=opts.ToolboxOpts(),
                legend_opts=opts.LegendOpts(is_show=False),
            )
            .render('resultFile/'+fileName)
        )   
        print(fileName+"已生成！")         


